package com.datastructure.test.combineofarray;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;

public class CombineOfArray {
    public ArrayList<ArrayList<Integer>> res = new ArrayList<>();
    public static void main(String[] args) {
        CombineOfArray combineOfArray = new CombineOfArray();
        ArrayList<ArrayList<Integer>> combine = combineOfArray.combine(3, 2);
        System.out.println(combine);
    }

    public ArrayList<ArrayList<Integer>> combine (int n, int k) {
        // write code here
        int[] data = new int[n];
        for (int i =1;i<=n;i++){
            data[i-1]=i;
        }
        Deque<Integer> deque = new ArrayDeque<>();
        computeCombine(data,0,deque,k);
        return res;
    }

    private void computeCombine(int[] data, int start,Deque<Integer> deque,int number) {
        if(deque.size()==number){
            res.add(new ArrayList<>(deque));
        }
        for (int i = start; i < data.length; i++) {
            deque.add(data[i]);
            computeCombine(data,i+1,deque,number);
            deque.removeLast();
        }
    }
}
